﻿using System;
using System.Collections.Generic;



namespace Kernelx.Contexts.ClientImpls
{
    using Core;

    internal class ClientWorkloadContext : IWorkloadStartupContext
    {

        public static ICurrentUser CurrentUser { get; set; }


        private IDictionary<string, object> _dataContainer;

        private Func<Type, object> _getService;
        private IRequestParameterCollection _requestParameters;
        private IEnumerable<FileWrapper> _fileWrappers;

        public ClientWorkloadContext(Func<Type, object> getService, IRequestParameterCollection defaultRequsetParameters)
        {
            _dataContainer = new Dictionary<string, object>();
            _getService = getService;
            _requestParameters = defaultRequsetParameters;
            _fileWrappers = new List<FileWrapper>();
        }


        public void Dispose()
        {
            _dataContainer?.Clear();
            _dataContainer = null;
            _getService = null;
            _requestParameters = null;
            _fileWrappers = null;
        }

        public ICurrentUser User => CurrentUser;

        public IRequestParameterCollection RequestParameters => _requestParameters;

        public IEnumerable<FileWrapper> FileWrappers => _fileWrappers;


        internal void SetStartupMessage(IRequestParameterCollection requestParameter, IEnumerable<FileWrapper> fileWrappers)
        {
            _requestParameters?.Dispose();
            _requestParameters = requestParameter;
            _fileWrappers = fileWrappers;
        }


        public object GetService(Type serviceType) => _getService(serviceType);

        public void SetData(string name, object data)
        {
            if (_dataContainer != null)
                _dataContainer[name] = data;
        }

        public object GetData(string name) => _dataContainer != null && _dataContainer.ContainsKey(name) ? _dataContainer[name] : null;
    }
}
